package atguigu2.collection;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/3
 * @VERSION 1.0
 * @DESC
 * 线程不安全示例：
 *
 * 1. ArrayList\HashSet\HashMap不安全
 *
 * 2. 解决办法
 * ...
 *
 * 3. 读时复制技术
 *
 *
 */
public class UnSafeDemo {
    public static void main(String[] args) {
        //
        List<String> list = new LinkedList<>();
        List<String> list1 =  new CopyOnWriteArrayList<>();//Collections.synchronizedList(new ArrayList<>()) ;//new Vector<>();//new ArrayList<>();
        //list1 = new Vector<>();

        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                list1.add(String.valueOf(UUID.randomUUID().toString()).substring(0,4));
                System.out.println(list1);
            },String.valueOf(i)).start();
        }//checkForComodification


        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<String>();//new HashSet<>();
        ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String,String>();




    }
}
